##########################################################################################

library(dplyr)
library(data.table)
library(optparse)

##########################################################################################

option_list <- list(
    make_option(c("--purity_file"), type = "character") ,
    make_option(c("--qc_file"), type = "character") ,
    make_option(c("--info_file"), type = "character") ,
    make_option(c("--out_file"), type = "character")
)

if(1!=1){
    
    work_dir <- "~/20220915_gastric_multiple/dna_combine/"
    purity_file <- "~/20220915_gastric_multiple/dna_combine/titan/Purity_titan.final.tsv"
    info_file <- "~/20220915_gastric_multiple/dna_combine/config/tumor_normal.class.list"
    qc_file <- paste(work_dir,"/Qc/Summary_Qc.tsv",sep="")
    out_file <- paste(work_dir,"/Qc/UseSampleAllInfo.tsv",sep="")

}

###########################################################################################

parseobj <- OptionParser(option_list=option_list, usage = "usage: Rscript %prog [options]")
opt <- parse_args(parseobj)
print(opt)

purity_file <- opt$purity_file
qc_file <- opt$qc_file
info_file <- opt$info_file
out_file <- opt$out_file

##########################################################################################

dat_qc <- data.frame(fread(qc_file))
dat_purity <- data.frame(fread(purity_file))
dat_info <- data.frame(fread(info_file))

##########################################################################################
tmp <- merge( dat_qc , dat_purity , by.x = "Tumor" , by.y = "Sample" )
tmp <- subset( tmp , Tumor %in% dat_info$Tumor )

write.table( tmp , out_file , row.names = F , sep = "\t" , quote = F )

